package com.zbkj.service.service.water.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zbkj.common.model.water.RzStore;
import com.zbkj.common.request.water.RzStoreSaveRequest;
import com.zbkj.common.request.water.RzStoreSaveReturn;
import com.zbkj.common.request.water.RzStoreSearchRequest;
import com.zbkj.service.dao.water.RzStoreDao;
import com.zbkj.service.service.water.RzStoreService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import cn.hutool.core.util.ObjectUtil;
import com.zbkj.common.exception.CrmebException;
import com.zbkj.common.result.CommonResultCode;

import javax.annotation.Resource;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zbkj.service.dao.water.RzDeviceDao;
import com.zbkj.common.model.water.RzDevice;

/**
 * RzStore 服务实现
 * +----------------------------------------------------------------------
 * | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2016~2025 https://www.crmeb.com All rights reserved.
 * +----------------------------------------------------------------------
 * | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
 * +----------------------------------------------------------------------
 * | Author: CRMEB Team <admin@crmeb.com>
 * +----------------------------------------------------------------------
 */
@Service
public class RzStoreServiceImpl extends ServiceImpl<RzStoreDao, RzStore> implements RzStoreService {

    @Resource
    private RzStoreDao rzStoreDao;

    @Resource
    private RzDeviceDao rzDeviceDao;

    @Override
    public PageInfo<RzStoreSaveReturn> getPage(RzStoreSearchRequest request) {
        PageHelper.startPage(request.getPage(), request.getLimit());
        List<RzStoreSaveReturn> list = rzStoreDao.selectPageList(request);

        // 为每个店铺加载设备列表
        for (RzStoreSaveReturn store : list) {
            List<RzDevice> devices = getDevicesByStoreId(store.getId());
            store.setDeviceList(devices);
        }

        return new PageInfo<>(list);
    }

    @Override
    public Boolean add(RzStoreSaveRequest request) {
        RzStore store = new RzStore();
        BeanUtils.copyProperties(request, store);
        store.setId(null);
        store.setIsDelete(false);
        boolean saved = save(store);
        if (saved) {
            bindDevices(store.getId(), request.getDeviceIds());
        }
        return saved;
    }

    @Override
    public Boolean updateStore(RzStoreSaveRequest request) {
        RzStore store = getById(request.getId());
        if (ObjectUtil.isEmpty(store) || Boolean.TRUE.equals(store.getIsDelete())) {
            throw new CrmebException(CommonResultCode.NOT_FOUND, "店铺不存在");
        }
        BeanUtils.copyProperties(request, store);
        boolean updated = updateById(store);
        if (updated) {
            bindDevices(store.getId(), request.getDeviceIds());
        }
        return updated;
    }

    @Override
    public Boolean deleteById(Integer id) {
        RzStore store = getById(id);
        if (ObjectUtil.isEmpty(store)) {
            throw new CrmebException(CommonResultCode.NOT_FOUND, "店铺不存在");
        }
        if (Boolean.TRUE.equals(store.getIsDelete())) {
            throw new CrmebException(CommonResultCode.VALIDATE_FAILED, "店铺已删除，禁止操作");
        }
        store.setIsDelete(true);
        return updateById(store);
    }

    @Override
    public List<RzDevice> getDevicesByStoreId(Integer storeId) {
        return rzStoreDao.selectDevicesByStoreId(storeId);
    }

    private void bindDevices(Integer storeId, List<Integer> deviceIds) {
        // 清空原有绑定
        LambdaUpdateWrapper<RzDevice> clearWrapper = Wrappers.lambdaUpdate();
        clearWrapper.set(RzDevice::getStoreId, 0).eq(RzDevice::getStoreId, storeId);
        rzDeviceDao.update(null, clearWrapper);
        // 绑定新设备
        if (deviceIds != null && !deviceIds.isEmpty()) {
            LambdaUpdateWrapper<RzDevice> bindWrapper = Wrappers.lambdaUpdate();
            bindWrapper.set(RzDevice::getStoreId, storeId).in(RzDevice::getId, deviceIds);
            rzDeviceDao.update(null, bindWrapper);
        }
    }
}
